package com.lvhx.springboot.config.shutdown;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ShutdownListener {
    private Logger logger = LoggerFactory.getLogger(ShutdownListener.class);

    @Autowired
    private ThreadPoolTaskExecutor partitionerExecutor;

    @Autowired
    private ThreadPoolTaskExecutor partitonHandlerExecutor;

    @EventListener(ContextClosedEvent.class)
    public void onShutdown(ContextClosedEvent event) {
        logger.info("==========进入服务关闭事件监听==========");
        // 先关闭A，再关闭B
        partitionerExecutor.shutdown();
        logger.info("==========partitionerExecutor已经关闭==========");

//        try {
//            executorA.getThreadPoolExecutor().awaitTermination(30, TimeUnit.SECONDS);
//        } catch (InterruptedException e) {
//            Thread.currentThread().interrupt();
//        }

        partitonHandlerExecutor.shutdown();
        logger.info("==========partitonHandlerExecutor已经关闭==========");

    }
}